# Plants are the fundamental units of the Plantae database.
# A plant consists of taxonomic information and associated traits.

require_dependency "search"

class Plant < ActiveRecord::Base
  belongs_to :family
  
  searches_on :all
  
  has_one :habitat, :dependent => true
  has_one :location, :dependent => true
  has_one :growth_form, :dependent => true
  has_one :perennation, :dependent => true
  has_one :successional_stage, :dependent => true
  has_one :sexual_system, :dependent => true
  has_one :self_sterility, :dependent => true
  has_one :primary_pollinator, :dependent => true
  has_one :nectar_production, :dependent => true
  has_one :dichogamy, :dependent => true
  has_one :seed_disperser, :dependent => true
  has_one :inflorescence_type, :dependent => true
  has_one :fruit_type, :dependent => true
  
  has_many :ovule_counts, :dependent => true
  has_many :open_seed_sets, :dependent => true
  has_many :hand_self_seed_sets, :dependent => true
  has_many :hand_cross_seed_sets, :dependent => true
  has_many :pollen_counts, :dependent => true
  has_many :flower_widths, :dependent => true
  has_many :seed_masses, :dependent => true
  has_many :seed_volumes, :dependent => true
  has_many :display_sizes, :dependent => true
  has_many :flower_productions, :dependent => true
  has_many :open_fruit_sets, :dependent => true
  has_many :hand_cross_fruit_sets, :dependent => true
  has_many :hand_self_fruit_sets, :dependent => true
  has_many :fruit_masses, :dependent => true
  has_many :total_inbreeding_depressions, :dependent => true
  has_many :pre_dispersal_inbreeding_depressions, :dependent => true
  has_many :post_dispersal_inbreeding_depressions, :dependent => true
  has_many :fruit_predations, :dependent => true
  has_many :seed_predations, :dependent => true
  
  validates_presence_of :genus, :specific_epithet, :family_id
  validates_uniqueness_of :specific_epithet, :scope => :genus,
                          :message => "This species already exists in Plantae."

  # Traits are associated into trait groups which are displayed together in views.
  # These arrays hold the trait groups by naming the specific models.
   
  GEOGRAPHY = [
    "location",
    "habitat"
  ].freeze
  
  ECOLOGY = [
    "growth_form",
    "perennation",
    "successional_stage"
  ].freeze
  
  BREEDING_SYSTEM = [
    "sexual_system",
    "self_sterility"
  ].freeze
  
  POLLINATION = [
    "primary_pollinator",
    "nectar_production"
  ].freeze
  
  GAMETE_PRODUCTION = [
    "ovule_count",
    "pollen_count"
  ].freeze
  
  INBREEDING_DEPRESSION = [
  "total_inbreeding_depression",
  "pre_dispersal_inbreeding_depression",
  "post_dispersal_inbreeding_depression"
  ].freeze
  
  PREDATION = [
  "fruit_predation",
  "seed_predation"
  ].freeze
  
  SEED_PRODUCTION = [
    "open_seed_set",
    "hand_self_seed_set",
    "hand_cross_seed_set",
    "open_fruit_set",
    "hand_cross_fruit_set",
    "hand_self_fruit_set",
    "fruit_mass"
  ].freeze
  
  # Provides the 10 most-recently modified plants for the sidebar.
  def self.recently_modified
    find(:all, :order => "updated_at asc", :limit => 10)
  end

end
